package com.meme.editor.property.control;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.util.List;

import java.io.File;

import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.meme.util.GuiUtil;

import org.apache.log4j.Logger;

import com.meme.editor.property.PropertyControl;

public class FileChooserPropertyControl extends PropertyControl implements ActionListener 
{
	static Logger logger = Logger.getLogger (FileChooserPropertyControl.class);
	
	public static final String Param_FileFilterParams = "FileNameExtensionFilter";
	
	JButton mButton;
	JTextField mTextField;
	JPanel mPanel;
	
	public FileChooserPropertyControl ()
	{
		
	}
	
	@Override
	public void enableEvents ()
	{
		mButton.addActionListener (this);
	}
	
	@Override
	public void disableEvents ()
	{
		mButton.removeActionListener (this);
	}
	
	protected void setText (String text)
	{
		getComponent (); // make sure mTextField != null and mButton != null
		
//		mButton.setText( text );
		if ( text != null && text.length() > 0 )
			mButton.setToolTipText ( text );
		else
			mButton.setToolTipText (null);
		
		mTextField.setText ( text );
	}
	
	@Override
	public void updateFromValue (Object[] controlParams) 
	{
		logger.assertLog (controlParams.length == 1, "the number of params is not correct.");
		logger.assertLog (controlParams[0] instanceof String, "the 1st param must be a String.");

		setText ( (String) controlParams[0] );
	}
	
	@Override
	public void updateFromValue ()
	{
		logger.error ("Shouldn't call this.");
	}
	
	@Override
	public Object[] makeDataForValue ()
	{
		getComponent (); // make sure mTextField != null
		
		return new Object[] 
		    {
				mTextField.getText()
		    };
		//return ( (JList)getComponent () ).getSelectedValues();
	}
	
//	@Override
//	public void setEditable (boolean bEditable)
//	{		
//		( (JButton)getComponent () ).setEnabled (bEditable);
//	}
	
	@Override
	public JComponent getComponent ()
	{
		if ( mPanel == null )
		{
			mPanel = new JPanel ();
			mPanel.setLayout ( new BorderLayout() );
			
			mButton = new JButton ("...");
			mButton.addActionListener (this);	
			mButton.setMargin (new Insets (0, 0, 0, 0));
			
			mTextField = new JTextField ();			
			mTextField.setEditable (isEditable ());
		}
		
		mPanel.removeAll();
		mPanel.add (mTextField, BorderLayout.CENTER);
		
		if ( isEditable () )
		{
			mPanel.add (mButton, BorderLayout.EAST);
		}

		return mPanel;
	}

	@Override
	public void disposeComponent ()
	{
		if ( mButton != null )
		{
			mButton.removeActionListener (this);
		}
		
		mTextField = null;
		mButton = null;
		mPanel = null;
	}
	
	@Override
	public void actionPerformed(java.awt.event.ActionEvent e)
	{
	    String filename = GuiUtil.popupFileChooser(getComponent ().getTopLevelAncestor(), "Open ...", true, mFileNameExtensions, mFileDescription, mTextField.getText());
    	if ( filename != null )	
	    {
	    	setText ( filename );
	    	
			updatePropertyValue ();
	    }
	}
	
	
	String mFileDescription = "Files";
	String[] mFileNameExtensions = {};
	
	@Override
	protected boolean _setParameter (String key, Object param)
	{
		if ( key.equals(Param_FileFilterParams) )
		{
			String params = (String) param;
			if (params != null)
			{
				String [] splitStrings = params.split(";");
				if (splitStrings.length > 1)
					mFileDescription = splitStrings [0];
				if (splitStrings.length > 2)
				{
					mFileNameExtensions = new String [splitStrings.length - 1];
					
					for (int i = 0; i < mFileNameExtensions.length; ++ i)
						mFileNameExtensions [i] = splitStrings[i+1];
				}
			}
			
			
			
			return true;
		}
		
		return false;
	}
}
